package com.qf.dao.impl;

import com.qf.dao.CartDao;
import com.qf.domain.Cartltem;
import com.qf.utils.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class CartDaoimpl implements CartDao {
    QueryRunner qr=new QueryRunner();
    //根据用户id和 商品id查询购物车所有信息
    @Override
    public Cartltem selectUserCart(Integer id, Integer goodsld) {
        Connection connection = DataSourceUtils.getConnection();
        String sql="select * from tb_cart where id=? and pid=?";
        try {
           return qr.query(connection,sql,new BeanHandler<>(Cartltem.class),id,goodsld);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw  new RuntimeException(throwables);
        }finally {
            DataSourceUtils.closeAll(null,null,connection);
        }

    }

    @Override
    public void UpdateCartGoods(Integer id, Integer goodsld, BigDecimal price, Integer s) {
        Connection connection = DataSourceUtils.getConnection();
        String sql="update tb_cart set num=?,money=? where id=? and pid=? ";
        try {
             qr.update(connection,sql,s,price,id,goodsld);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw  new RuntimeException(throwables);
        }finally {
            DataSourceUtils.closeAll(null,null,connection);
        }
    }

    @Override
    public void InsertUserCart(Integer id, Integer goodsld, Integer number1, BigDecimal price) {
        Connection connection = DataSourceUtils.getConnection();
        String sql="insert into tb_cart(id,pid,num,money)values(?,?,?,?)";
        try {
            qr.update(connection,sql,id,goodsld,number1,price);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw  new RuntimeException(throwables);
        }finally {
            DataSourceUtils.closeAll(null,null,connection);
        }
    }

    @Override
    public List<Cartltem> selectUserCartAll(Integer id) {
        Connection connection = DataSourceUtils.getConnection();
        String sql="select * from tb_cart where id=?";
        try {
           return qr.query(connection,sql,new BeanListHandler<>(Cartltem.class),id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw  new RuntimeException(throwables);
        }finally {
            DataSourceUtils.closeAll(null,null,connection);
        }
    }

    @Override
    public void deleteUserCart(Integer id, Integer pid) {
        Connection connection = DataSourceUtils.getConnection();
        String sql="delete from tb_cart where id=? and pid=?";
        try {
            qr.update(connection,sql,id,pid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw  new RuntimeException(throwables);
        }finally {
            DataSourceUtils.closeAll(null,null,connection);
        }
    }
}
